//
//  UserInfo.swift
//  LoveFreshBeen
//
//  Created by Gao on 17/2/9.
//  Copyright © 2017年 Odm. All rights reserved.
//

import UIKit

class UserInfo: NSObject {
    
    fileprivate static let instance = UserInfo()
    
    fileprivate var allAdress: [Adress]?
    
    class var sharedUserInfo: UserInfo {
        return instance
    }
    
    func hasDefaultAdress() -> Bool {
        
        if allAdress != nil {
            return true
        } else {
            return false
        }
    }
    
    func setAllAdress(_ adresses: [Adress]) {
        allAdress = adresses
    }
    
    func cleanAllAdress() {
        allAdress = nil
    }
    
    func defaultAdress() -> Adress? {
        if allAdress == nil {
            weak var tmpSelf = self
            
            AdressData.loadMyAdressData { (data, error) -> Void in
                
                if let data = data, let datadata = data.data {
                    
                    if datadata.count > 0 {
                        tmpSelf!.allAdress = datadata
                    } else {
                        tmpSelf?.allAdress?.removeAll()
                    }

                }else {
                    tmpSelf?.allAdress?.removeAll()
                }

                
            }
            guard let allAdress = allAdress else {return nil}
            return allAdress.count > 1 ? allAdress[0] : nil
            
        } else {
            return allAdress![0]
        }
    }
    
    func setDefaultAdress(_ adress: Adress) {
        if allAdress != nil {
            allAdress?.insert(adress, at: 0)
        } else {
            allAdress = [Adress]()
            allAdress?.append(adress)
        }
    }
}
